package cn.v2tila.rsosa.management.controller;

import cn.v2tila.rsosa.management.entity.Yusibin;
import cn.v2tila.rsosa.management.jpa.YusibinJPA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "yusibin")
public class yusibinController {

    @Autowired
    private YusibinJPA yusibinJPA;

    @RequestMapping(value = "hello")
    public void helloworld(){
        System.out.println("hello world");
    }


    @RequestMapping(value = "save")
    /**
     * @param
     * @return int
     * @author v2tila
     * @creed: Talk is cheap,show me the code
     * @date 2019/1/8 16:26
     */
    public int save() {
        List<Yusibin> yusibins = new ArrayList<Yusibin>();
        String name = "";
        for (int i = 0; i < 20; i++) {
            name = "yusibin" + i;
            Yusibin yusibin1 = new Yusibin();
            yusibin1.setName(name);
            yusibins.add(yusibin1);
        }
        System.out.println(yusibins);
        for (Yusibin yusibin : yusibins) {
            yusibinJPA.save(yusibin);
        }

        return 1;

    }

    @RequestMapping(value = "findAll")
    public List<Yusibin> findAll(Yusibin yusibin) {
        return yusibinJPA.findAll();
    }


    @RequestMapping("pageFindAll")
    /**
     * @param
     * @return java.util.List<cn.v2tila.rsosa.management.entity.Yusibin>
     * @author v2tila
     * @creed: Talk is cheap,show me the code
     * @date 2019/1/8 16:25
     */
    public List<Yusibin> page(){

        Sort sort =new Sort(Sort.Direction.ASC,"id");
        //新方法 代替PageRequest
        Pageable pageable = PageRequest.of(1, 5, sort);
        Page<Yusibin> page = yusibinJPA.findAll(pageable);
        List<Yusibin> yusibinList = page.getContent();
        //数据总页数
        int toto = page.getTotalPages();
        System.out.println(toto);
        return yusibinList;

    }



}
